﻿/// File: ViewletControls\tooltip.fs
/// 
/// Author: Giacomo Righetti (righetti@di.unipi.it)
///   
/// ------------------------------------------------------------
/// Copyright (c) Microsoft Corporation.  All rights reserved.
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------

namespace LightweightControls

open System.Drawing
open System.Windows.Forms

// FIX: add more cases
type ToopTipStyle = OldStyle | System

///
/// Summary:
///     The lightweight version of the tooltip control.
type LightweightToolTip() as x =
    inherit LightweightControl()
        
    let timer : Timer = new Timer(Interval = 10)
    
    let mutable (dx,dy) = (0.0f, 0.0f)    
        
    let mutable active = true    
    let mutable automaticDelay = 500
    
    let mutable popupTime = 200
    
    let mutable text = ""
    
    ///
    /// Summary:
    ///     Gets or sets a value indicating whether the LightweightToolTip is currently active.
    ///
    /// Returns:
    ///     true if the LightweightToolTip is currently active; otherwise, false. The default is
    ///     true.    
    member x.Active
        with get() = active
        and set(value) = active <- value
        
    ///
    /// Summary:
    ///     Gets or sets the automatic delay for the ToolTip.
    ///
    /// Returns:
    ///     The automatic delay, in milliseconds. The default is 500.    
    member x.AutomaticDelay
        with get() = automaticDelay
        and set(value) = automaticDelay <- value
                
    member x.PopupTime
        with get() = popupTime
        and set(value) = popupTime <- value
    
    member x.Text
        with get() = text
        and set(value) = text <- value
    
    member x.Show() =
        let numActivations = popupTime / timer.Interval
        dx <- float32 x.Size.Width / float32 popupTime * float32 timer.Interval
        dy <- float32 x.Size.Height / float32 popupTime * float32 timer.Interval
        let tmp = ref 0
        timer.Tick.Add(fun _ ->
            tmp := !tmp + 1
            x.Size <- Size(x.Size.Width + int dx, x.Size.Height + int dy)
            x.Invalidate(Rectangle(x.Position.X, x.Position.Y, x.Size.Width + 1, x.Size.Height + 1))
            if !tmp = numActivations then timer.Stop()
            // popup has been completed.
        )
        timer.Start()
        
    override x.OnPaint (e) = 
        let g = e.Graphics
        g.FillRectangle(SystemBrushes.Info, x.ClientRectangle)
        g.DrawRectangle(Pens.Black, x.ClientRectangle)